//
//  CController.m
//  TheRicher
//
//  Created by 李晓东 on 2018/2/5.
//  Copyright © 2018年 PeanutXu. All rights reserved.
//

#import "CController.h"
#import "DatabaseResultDelegate.h"
#import "CCell.h"

@interface CController () <DatabaseResultDelegate>

@end

@implementation CController
- (instancetype)init{
    if (self = [super init]) {
        self.netService = [NetService share];
    }
    return self;
}

- (void)loadView{
    [super loadView];
    
    self.dataArray = [NSMutableArray array];
    self.databaseService = [DatabaseService share];
    
    self.netQueryResultBlock = ^(id resultString) {
        //        [self.tableView reloadData];
        
    };
    [self.tableView setDataSource:self];
    [self.tableView registerClass:[CCell class] forCellReuseIdentifier:@"TableCellId"];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 1.先查询数据库
    self.databaseService.delegate = self;
    self.tableName = ShuangSeQiuTableName;
    [self.databaseService queryTable:ShuangSeQiuTableName withCondition:nil ofConditionName:nil andJudgeFlag:nil andOrderBy:@"issueno" andSortBy:1 andCountOf:RowCount];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCellId" forIndexPath:indexPath];
    [cell setValues:self.dataArray[indexPath.row]];
    [cell setSelectedBackgroundView:[[UIView alloc]initWithFrame:cell.bounds ]];
    [cell.selectedBackgroundView setBackgroundColor:[UIColor redColor]];
    
    return cell;
}

- (void)resultInDatabase:(NSArray *)resultArray{
    _callWhichMethodBlock(resultArray);
    if ([self.dataArray count] > 0) {
        if ([[(ShuangSeQiu *)self.dataArray.lastObject number] containsString:[resultArray[0] killingNumber]]) {
            [(ShuangSeQiu *)self.dataArray.lastObject setDeadthNumber:[resultArray[0] killingNumber]];
        }
    }
    for (int i = 0; i < resultArray.count - 1; i++ ) {
        if ([[(ShuangSeQiu *)resultArray[i] number] containsString:[(ShuangSeQiu *)resultArray[i + 1] killingNumber]] ) {
            [(ShuangSeQiu *)resultArray[i] setDeadthNumber:[(ShuangSeQiu *)resultArray[i + 1] killingNumber]];
        }
    }
    [self.dataArray addObjectsFromArray:resultArray];
    [self.tableView reloadData];
    
    if ([self.activityIndicatorView isAnimating]) {
        [self.activityIndicatorView stopAnimating];
    }
}

- (void)typeMethodInKillNumberTools:(NSArray *)resultArray{
    _callWhichMethodBlock(resultArray);
    
}

- (void)judgeKillingNumber:(NSInteger)resultCount{
    for (int i = 0; i < resultCount + 1; i++) {
        if ([(ShuangSeQiu *)self.dataArray[i] judgeWhetherItContain:[(ShuangSeQiu *)self.dataArray[i + 1] killingNumber
                                                                     ]]) {
            [(ShuangSeQiu *)self.dataArray[i] setDeadthNumber:[(ShuangSeQiu *)self.dataArray[i + 1] killingNumber]];
        }
        
    }
    self.needToRefreshCount = 0;
    [self.tableView reloadData];
}

- (void)refreshChildControllerView:(NSInteger)refreshCount{
    [self judgeKillingNumber:refreshCount];
}

- (void)dealloc{
    _callWhichMethodBlock = nil;
    NSLog(@"done in dealoc");
}
@end
